<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的店铺</title>
    <link href="../asserts/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    <script src="../asserts/plugins/jquery-3.6.0.min.js"></script>
    <script src="../asserts/plugins/bootstrap/js/bootstrap.js"></script>

    <script>
        $(function () {
            // 获取展示所有商品
            function getCommoditys(){
                let $commodityList = $("#commodityList").text("");
                $.ajax({
                    url:'/KunKunMarket/CommodityController',
                    type:'get',
                    dataType:"json",
                    data:{
                        type: 'getCommodityListByUserId'
                    },
                    success:function (respBean) {
                        console.log(respBean);
                        if (respBean.status == 500){
                            alert(respBean.msg);
                            return;
                        }
                        //更新前端分页组件信息
                        currentPage = respBean.data.currentPage;
                        len = respBean.data.len;
                        $("#currentPage").text(currentPage);
                        $("input[name=len]").text(len);


                        //获取商品，添加到元素中
                        for (let i=0; i<respBean.data.length; i++){
                            //创建html标签元素
                            $commodity = $(`
                            <div class="col-lg-3 col-md-4 col-sm-6 col-xs-6">
                                <div class="thumbnail">
                                    <img id="commodity_img" style="width: 255px;height: 290px" src="../asserts/img/1673342375030323477.jpg" alt="..."class="img-responsive">
                                    <div class="caption">
                                        <h4 id="commodity_name"  >华为畅享7</h4>
                                        <p style="color:red;">
                                            <span>价格：</span> <span id="commodity_price" ></span>
                                        </p>
                                        <p >
                                            <span>库存：</span> <span id="commodity_stock" ></span>
                                        </p>
                                        <p id="commodity_id" style="color:red;display: none"></p>
                                        <p id="commodity_user_id" style="color:red;display: none"></p>
                                        <button data-target="#tanChuang"  class="updateMyCommodity btn btn-default" style="width: 75px" data-toggle="modal">编辑</button>
                                        <button class="deleteMyCommodity btn btn-danger btn-xs" style="float: right;margin-top: 7px">删除</button>
                                    </div>
                                </div>
                            </div>
                            `);
                            $commodity.find("#commodity_img").attr("src",respBean.data[i].img_url);
                            $commodity.find("#commodity_name").text(respBean.data[i].name);
                            $commodity.find("#commodity_price").text(respBean.data[i].price);
                            $commodity.find("#commodity_stock").text(respBean.data[i].stock)

                            $commodity.find("#commodity_id").text(respBean.data[i].id)
                            $commodity.find("#commodity_user_id").text(respBean.data[i].user_id)

                            //将新创建的元素添加到商品列表容器
                            $commodityList.append($commodity);
                        }

                        // 给每个商品内的按钮添加点击事件
                        $(".updateMyCommodity").click(updateMyCommodity);
                        $(".deleteMyCommodity").click(deleteMyCommodity);
                    }
                })
            }
            getCommoditys();
            //编辑商品信息
            function updateMyCommodity() {
                //获取到商品id, 设置到表单的异常域中
                let commodity_id = $(this).parents(".caption").find("#commodity_id").text();
                $("input[name=commodity_id]").val(commodity_id);
            }
            //编辑商品信息，发送请求到后端
            $("button[name=updateMyCommodity]").click(function () {
                $.ajax({
                    url:"/KunKunMarket/CommodityController",
                    type:"get",
                    dataType:"json",
                    data: {
                        type:'updateCommodityById',
                        id: $("#tanChuang").find("input[name=commodity_id]").val(),
                        name: $("#tanChuang").find("input[name=name]").val(),
                        stock: $("#tanChuang").find("input[name=stock]").val(),
                        price: $("#tanChuang").find("input[name=price]").val()
                    },
                    success:function (respBean) {
                        alert(respBean.msg);
                        if (respBean.status == 200){
                            $(".cel").click();
                            getCommoditys();
                        }
                    }
                })
            })
            //删除商品
            function deleteMyCommodity() {
                console.log("删除商品");
                //获取到商品id, 删除商品
                let commodity_id = $(this).parents(".caption").find("#commodity_id").text();

                $.ajax({
                    url:"/KunKunMarket/CommodityController",
                    type:"get",
                    dataType:"json",
                    data: {
                        type:'deleteCommodityById',
                        id: commodity_id
                    },
                    success:function (respBean) {
                        alert(respBean.msg);
                        if (respBean.status == 200){
                            getCommoditys();
                        }
                    }
                })
            }

            //添加商品
            /*
            String name = req.getParameter("name");
            String stock = req.getParameter("stock");
            String price = req.getParameter("price");
            Part file_img = req.getPart("file_img");
             */
            $("button[name=addCommodity]").click(function () {
                console.log("添加商品");
                let img_ele = $("#tanChuang2").find("input[name=file_img]")[0];
                let file = img_ele.files[0];
                let formData = new FormData();
                formData.append("type", "addCommodity");
                formData.append("name", $("#tanChuang2").find("input[name=name]").val());
                formData.append("stock", $("#tanChuang2").find("input[name=stock]").val());
                formData.append("price", $("#tanChuang2").find("input[name=price]").val());
                formData.append("file_img", file);
                $.ajax({
                    url:"/KunKunMarket/CommodityController",
                    type:"post",
                    contentType:false,
                    processData: false,
                    data:formData,
                    dataType:"json",
                    success:function (respBean) {
                        alert(respBean.msg);
                        if (respBean.status == 200){
                            location.href = location.href;
                            getCommoditys();
                        }
                    }
                })
            })
        })
    </script>

</head>
<body>
<div class="container">
    <a href="/KunKunMarket/UserController?type=logout">退出登录</a>
    <!--bootstrap实现弹出模态框-->
    <button class="btn btn-default" data-target="#tanChuang2" data-toggle="modal">
        点击添加商品
    </button>
    <!-- 张略缩图开始 -->
    <div class="row" id="commodityList">

    </div>


    <!--fade 弹出表单,修改商品信息-->
    <div class="modal fade"  id="tanChuang" aria-labelledby="myModallabel" aria-hidden="true" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h5 class="modal-title" id="myModallabel">编辑该商品的信息</h5>
                </div>
                <form id="formData" class="form-horizontal">
                    <input type="hidden" name="commodity_id">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="control-label col-lg-2">商品名：</label>
                            <div class="col-lg-9">
                                <input type="text" value="" name="name" class="form-control"></br>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-2">库存：</label>
                            <div class="col-lg-9">
                                <input type="number" value="" name="stock" class="form-control"></br>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-2">价格：</label>
                            <div class="col-lg-9">
                                <input type="text" value="" name="price" class="form-control">
                            </div>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button class="cel btn btn-default" type="button" data-dismiss="modal">取消</button>
                        <button class="btn btn-success" type="button" name="updateMyCommodity">确认</button>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <!--fade 弹出表单, 添加商品-->
    <div class="modal fade"  id="tanChuang2" aria-labelledby="myModallabel" aria-hidden="true" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h5 class="modal-title">添加商品</h5>
                </div>
                <form class="form-horizontal">
                    <input type="hidden" name="commodity_id">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="control-label col-lg-2">商品名：</label>
                            <div class="col-lg-9">
                                <input type="text" value="" name="name" class="form-control"></br>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-2">库存：</label>
                            <div class="col-lg-9">
                                <input type="number" value="" name="stock" class="form-control"></br>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-2">价格：</label>
                            <div class="col-lg-9">
                                <input type="text" value="" name="price" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-2">图片：</label>
                            <div class="col-lg-9">
                                <input type="file" value="" name="file_img" class="form-control">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button class="cel btn btn-default" type="button" data-dismiss="modal">取消</button>
                        <button class="btn btn-success" type="button" name="addCommodity">确认</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

</div>



</body>
</html>